<?php
class SleekBase_BreadcrumbsModule extends Module {
	public function run () {
		$crumbs = $this->getBreadcrumbs();

		if ($crumbs) {
			$this->setData('crumbs', $crumbs);
		}
		else {
			$this->setView(false);
		}
	}

	private function getBreadcrumbs () {
		$cols		= array();
		$cols[0]	= array(
			'title'	=> conf('general.site_title'), 
			'url'	=> url('Home')
		);
		$dirs		= explode('/', SleekPHP::request());
		$validDirs	= array();
		$i			= 1;
		$prevUrl	= '';

		foreach ($dirs as $dir) {
			if (strlen($dir) > 0 and $dir != 'index.php' and substr($dir, 0, 1) != '?') {
				$validDirs[] = $dir;
			}
		}

		$dirs		= $validDirs;
		$numDirs	= count($dirs);

		foreach ($dirs as $dir) {
			$langPrefix	= (SleekPHP::currentLang() == conf('lang.default_lang')) ? '' : SleekPHP::currentLang() . '/';
			$url		= ($i == $numDirs ? false : str_replace('//', '/', (MOD_REWRITE ? WEBROOT : WEBROOT . 'index.php/') . "$langPrefix{$prevUrl}/$dir/"));
			$cols[$i++]	= array(
				'title'	=> ucwords(str_replace('-', ' ', $dir)), 
				'url'	=> $url
			);
			$prevUrl	= "$prevUrl/$dir";
		}

		return count($cols) > 1 ? $cols : false;
	}
}
